﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

namespace ShareMarking.Services
{
   public class zip_http
    {
        /// <summary>
        /// 检查URL是否可访问(返回状态码)
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
       public static int GetStatusCode(string url)
        {
            WebRequest Http = WebRequest.Create(url);
            Http.Method = "HEAD";//设置Method为HEAD
            int StatusCode = 0;
            try
            {
                HttpWebResponse response = (HttpWebResponse)Http.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK) //200  
                {
                    StatusCode = Convert.ToInt32(response.StatusCode);//Statuscode 为枚举类型，200为正常，其他输出异常，需要转为int型才会输出状态码
                }
                else if (response.StatusCode == HttpStatusCode.BadRequest)//400  
                {
                    StatusCode = Convert.ToInt32(response.StatusCode);
                }
                else if (response.StatusCode == HttpStatusCode.NotFound)//404  
                {
                    StatusCode = Convert.ToInt32(response.StatusCode);
                }
                else if (response.StatusCode == HttpStatusCode.InternalServerError)//500  
                {
                    StatusCode = Convert.ToInt32(response.StatusCode);
                }
                else
                {
                    StatusCode = Convert.ToInt32(response.StatusCode);
                }
                response.Close();
            }
            catch (WebException ex)
            {
                ComLib.LogLib.Log4NetBase.Log("0-0---------------图片不可访问:" + ex.Message + ex.StackTrace + ((HttpWebResponse)ex.Response).StatusCode.ToString() + "---------------");
                StatusCode = Convert.ToInt32(((HttpWebResponse)ex.Response).StatusCode);
            }
            return StatusCode;
        }

        /// <summary>
        /// zip打包方法（这要除去它是完整的文件路径条目的目录部分。）
        /// </summary>
        /// <param name="folderPath">保存的文件路径</param>
        /// <param name="zipFilePath">zip文件名称</param>
        public static void ZipFolderContents(string folderPath, string zipFilePath)
        {
            string[] files = Directory.GetFiles(folderPath);
            using (ZipFile zipFile = ZipFile.Create(zipFilePath))
            {
                zipFile.NameTransform = new ZipNameTransform(folderPath);
                foreach (string file in files)
                {
                    zipFile.BeginUpdate();
                    zipFile.Add(file);
                    zipFile.CommitUpdate();
                }
            }
        }
    }
}
